import {createStore} from 'vuex'

export default createStore({
    state:{
       userName:localStorage.getItem('userName')? localStorage.getItem('userName'):'',
       age:10,
       cd:[
           {name:'花菜',pri:10},
           {name:'芹菜',pri:20},
           {name:'白菜',pri:10},
       ]
    },
    getters:{
        nameLength(state){
            return state.userName?.length
        },
        cdlength(state){
            return state.cd.length
        }
    },
    mutations:{
        login(state,name){
            state.userName=name
            localStorage.setItem('userName',name)
        },
        setAge(state,age){
           state.age+=age
        },
        addc(state,cd){
            state.cd.push(cd)
        }
    },
    actions:{
        login({commit},name){
           commit('login',name)
        },
        setAge({commit},age){
            commit('setAge',age)
        }
    }
})